/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 ConnectionHandler.hpp. Created on: 19.08.2009 12:01:11
 */

#ifndef CONNECTIONHANDLER_HPP_
#define CONNECTIONHANDLER_HPP_

#include "smartobjects/SmartObjectsDefs.hpp"
#include "IConnection.hpp"
#include "IEndpointCoordinator.hpp"
#include "IManagedConnection.hpp"
#include "IConnectionListener.hpp"
#include "IStreamListener.hpp"

namespace smartobjects {

class ConnectionHandler : public IConnectionListener, public IStreamListener, public virtual RefObject {
public:
	SO_DLLPRFX ConnectionHandler(IConnection* poConnection, IEndpointCoordinator* poEndpointCoordinator);
	SO_DLLPRFX virtual ~ConnectionHandler();

	SO_DLLPRFX void setManagedConnection(IManagedConnection*);
	SO_DLLPRFX IManagedConnection* getManagedConnection();
	SO_DLLPRFX void resetManagedConnection();
	SO_DLLPRFX IConnection* getConnection();
	SO_DLLPRFX IDataEncrypter* selectDataEncrypter();
	SO_DLLPRFX IEndpointCoordinator* getCoordinator();

	SO_DLLPRFX virtual void onConnected(IConnection* poConnection) throw();
	SO_DLLPRFX virtual void onDisconnected(IConnection* poConnection) throw();
	SO_DLLPRFX virtual void onConnectionFailure(IConnection* poConnection, const std::string& sError) throw();

	SO_DLLPRFX virtual void onReceived(IConnection* poConnection, cpputils::io::ByteBuffer*) throw();
	SO_DLLPRFX virtual void onReceiveError(IConnection* poConnection, const std::string& sErrorMessage) throw();
	SO_DLLPRFX virtual void onWriteError(IConnection* poConnection, const std::string& sErrorMessage) throw();

private:
	IConnection* m_poConnection;
	boost::recursive_mutex m_mxDataEncrypter;
	IDataEncrypter* m_poDataEncrypter;
	IEndpointCoordinator* m_poEndpointCoordinator;
	IManagedConnection* m_poManagedConnection;
};

}

#endif /* CONNECTIONHANDLER_HPP_ */
